View Javadoc
1   package org.apache.maven.surefire.testng;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.surefire.report.CategorizedReportEntry;
23  import org.apache.maven.surefire.report.PojoStackTraceWriter;
24  import org.apache.maven.surefire.report.ReportEntry;
25  import org.apache.maven.surefire.report.RunListener;
26  import org.apache.maven.surefire.report.SimpleReportEntry;
27  
28  import org.testng.ISuite;
29  import org.testng.ISuiteListener;
30  import org.testng.ITestContext;
31  import org.testng.ITestListener;
32  import org.testng.ITestResult;
33  
34  import static org.apache.maven.surefire.report.SimpleReportEntry.ignored;
35  import static org.apache.maven.surefire.report.SimpleReportEntry.withException;
36  
37  /**
38   * Listens for and provides and adaptor layer so that
39   * TestNG tests can report their status to the current
40   * {@link org.apache.maven.surefire.report.RunListener}.
41   *
42   * @author jkuhnert
43   * @noinspection ThrowableResultOfMethodCallIgnored
44   */
45  public class TestNGReporter
46      implements ITestListener, ISuiteListener
47  {
48      private final RunListener reporter;
49  
50      /**
51       * Constructs a new instance that will listen to
52       * test updates from a {@link org.testng.TestNG} class instance.
53       * <p/>
54       * <p/>It is assumed that the requisite {@link org.testng.TestNG#addListener(ITestListener)}
55       * method call has already associated with this instance <i>before</i> the test
56       * suite is run.
57       *
58       * @param reportManager Instance to report suite status to
59       */
60      public TestNGReporter( RunListener reportManager )
61      {
62          this.reporter = reportManager;
63      }
64  
65      public void onTestStart( ITestResult result )
66      {
67          String group = groupString( result.getMethod().getGroups(), result.getTestClass().getName() );
68          String userFriendlyTestName = getUserFriendlyTestName( result );
69          reporter.testStarting( new CategorizedReportEntry( getSource( result ), userFriendlyTestName, group ) );
70      }
71  
72      private String getSource( ITestResult result )
73      {
74          return result.getTestClass().getName();
75      }
76  
77      public void onTestSuccess( ITestResult result )
78      {
79          ReportEntry report = new SimpleReportEntry( getSource( result ), getUserFriendlyTestName( result ) );
80          reporter.testSucceeded( report );
81      }
82  
83      public void onTestFailure( ITestResult result )
84      {
85          ReportEntry report = withException( getSource( result ), getUserFriendlyTestName( result ),
86                  new PojoStackTraceWriter( result.getTestClass().getRealClass().getName(),
87                          result.getMethod().getMethodName(),
88                          result.getThrowable() ) );
89  
90          reporter.testFailed( report );
91      }
92  
93      private static String getUserFriendlyTestName( ITestResult result )
94      {
95          // This is consistent with the JUnit output
96          return result.getName() + "(" + result.getTestClass().getName() + ")";
97      }
98  
99      public void onTestSkipped( ITestResult result )
100     {
101         Throwable t = result.getThrowable();
102         String reason = t == null ? null : t.getMessage();
103         ReportEntry report = ignored( getSource( result ), getUserFriendlyTestName( result ), reason );
104         reporter.testSkipped( report );
105     }
106 
107     public void onTestFailedButWithinSuccessPercentage( ITestResult result )
108     {
109         ReportEntry report = withException( getSource( result ), getUserFriendlyTestName( result ),
110                 new PojoStackTraceWriter( result.getTestClass().getRealClass().getName(),
111                         result.getMethod().getMethodName(),
112                         result.getThrowable() ) );
113 
114         reporter.testSucceeded( report );
115     }
116 
117     public void onStart( ITestContext context )
118     {
119 
120     }
121 
122     public void onFinish( ITestContext context )
123     {
124 
125     }
126 
127 
128     public void onStart( ISuite suite )
129     {
130 
131     }
132 
133     public void onFinish( ISuite suite )
134     {
135 
136     }
137 
138     /**
139      * Creates a string out of the list of testng groups in the
140      * form of <pre>"group1,group2,group3"</pre>.
141      *
142      * @param groups       The groups being run
143      * @param defaultValue The default to use if no groups
144      * @return a string describing the groups
145      */
146     private static String groupString( String[] groups, String defaultValue )
147     {
148         String retVal;
149         if ( groups != null && groups.length > 0 )
150         {
151             StringBuilder str = new StringBuilder();
152             for ( int i = 0; i < groups.length; i++ )
153             {
154                 str.append( groups[i] );
155                 if ( i + 1 < groups.length )
156                 {
157                     str.append( "," );
158                 }
159             }
160             retVal = str.toString();
161         }
162         else
163         {
164             retVal = defaultValue;
165         }
166         return retVal;
167     }
168 
169     public void onConfigurationFailure( ITestResult result )
170     {
171         onTestFailure( result );
172     }
173 
174     public void onConfigurationSkip( ITestResult result )
175     {
176         onTestSkipped( result );
177     }
178 
179     public void onConfigurationSuccess( ITestResult result )
180     {
181         // DGF Don't record configuration successes as separate tests
182         //onTestSuccess( result );
183     }
184 
185 }